//
//  RightTableViewCell.m
//  Linkage
//
//  Created by LeeJay on 16/8/22.
//  Copyright © 2016年 LeeJay. All rights reserved.
//  代码下载地址https://github.com/leejayID/Linkage
//h -  30 = 700
#import "RightTableViewCell.h"
#import "CategoryModel.h"
#import "NSAttributedString+YYText.h"
@interface RightTableViewCell ()

@property (nonatomic, strong) UIImageView *imageV; //1.图片
@property (nonatomic, strong) UILabel *nameLabel; //2.商品名
@property (nonatomic, strong) UILabel *priceLabel;//3.商品价格 欧元

/**原价灰色，灰色 中划线 12-5隐藏 */
@property (nonatomic,  strong) UILabel *originPriceLb;

//11-19页面修改
//@property (nonatomic, strong) UIButton *addBtn; //4.添加按钮
@end

@implementation RightTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self setupView];
        //取消点击样式
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

#pragma mark - 设置数据
- (void)setDetailModel:(WaiSongDetailModel *)detailModel {
    _detailModel = detailModel;
    
    NSURL *url = [NSURL URLWithString:detailModel.thumbnail];
    [self.imageV sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    //不知道占位图张什么样, 不能有假数据。r l go le ya
    
    NSString *fenStr = NSLocalizedString(L_Order_Fen, @"份");

    self.nameLabel.text = detailModel.title;
    self.priceLabel.text = [NSString stringWithFormat:@"€%.1f/%@",([detailModel.currentPrice floatValue]),fenStr];
    
    //需求:一个控件显示两种颜色的文字
    //富文本:灰色+中划线
    //￥1.0/份数￥1.0
    //￥1.0/份数 ￥1.0(灰色+中划线)
    
    //前一段 现价
    NSString *currentStr = [NSString stringWithFormat:@"€%.1f/%@",[detailModel.currentPrice floatValue],fenStr];
    self.priceLabel.text = currentStr;
    
    
    //后一段 原价 灰色+中划线
    NSString *originPriceStr = [NSString stringWithFormat:@"€%.1f",[detailModel.originalPrice floatValue]];
    NSMutableDictionary *attributeDicM = @{}.mutableCopy;
    attributeDicM[NSStrikethroughStyleAttributeName] = [NSNumber numberWithUnsignedInteger:NSUnderlineStyleSingle]; //中划线
    NSMutableAttributedString *attM = [[NSMutableAttributedString alloc] initWithString:originPriceStr attributes:attributeDicM];
    self.originPriceLb.attributedText = attM;
    
    //赋值
//    self.priceLabel.attributedText = attStrM;
    //更新约束:计算文字宽度
//    CGSize originPriceLbSize = [originPriceStr sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0]}];
//    if (originPriceLbSize.width) { //如果有文字就显示
//        [self.originPriceLb mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.priceLabel.mas_right);
//            make.right.equalTo(self.contentView.mas_right).offset(-10);
//            make.top.equalTo(self.nameLabel.mas_bottom);
//
//            make.width.mas_equalTo(originPriceLbSize.width);
//        }];
//    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setupView {
    [self.contentView addSubview:self.imageV];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.priceLabel];
    [self.contentView addSubview:self.originPriceLb];
//    [self.contentView addSubview:self.addBtn];
}

#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    //图片
    [self.imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.size.mas_equalTo(CGSizeMake(FDWidthScale(80), FDHeightScale(65)));
        make.centerY.equalTo(self.contentView.mas_centerY);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageV.mas_top);
        make.left.equalTo(self.imageV.mas_right).offset(10);
        make.right.equalTo(self.contentView.mas_right).offset(-10);
        make.bottom.equalTo(self.priceLabel.mas_top);
    }];
    
    //参考上一个控件 实际显示的view
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).offset(-10);
        make.size.mas_equalTo(CGSizeMake(120, 22));
        make.bottom.equalTo(self.imageV.mas_bottom).offset(-5);
    }];
}

#pragma mark -  getter
- (UIImageView *)imageV {
    if (!_imageV) {
        _imageV = [UIImageView new];
    }
    return _imageV;
}
- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.font = [UIFont systemFontOfSize:15.0];
        //文字右边对其
        _nameLabel.textAlignment = NSTextAlignmentRight;
        _nameLabel.numberOfLines = 0;
    }
    return _nameLabel;
}
- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [UILabel new];
        _priceLabel.font = [UIFont systemFontOfSize:15.0];
        _priceLabel.textAlignment = NSTextAlignmentRight;
    }
    return _priceLabel;
}
- (UILabel *)originPriceLb {
    if (!_originPriceLb) {
        _originPriceLb = [UILabel new];
        _originPriceLb.textColor = [UIColor lightGrayColor];
        _originPriceLb.font = [UIFont systemFontOfSize:15.0];
        _originPriceLb.hidden = YES;
    }
    return _originPriceLb;
}


#pragma mark - 点击事件
- (void)clickAddBtnAction:(UIButton *)btn {
    //默认数量为1
    if (self.clickAddBtnBlock) {
        self.clickAddBtnBlock(1);
    }
}

@end
